package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.scd.bean.Order;
import com.kgc.scd.bean.OrderComment;
import com.kgc.scd.bean.OrderVO;
import com.kgc.scd.mapper.OrderMapper;
import com.kgc.scd.service.OrderSerivce;
import com.kgc.scd.util.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: WangKuoKuo
 * @create: 2022-06-10 15:17
 * @description:
 **/
@Service
public class OrderSerivceImpl extends ServiceImpl<OrderMapper, Order> implements OrderSerivce {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<OrderVO> queryOrderList(String orderId) {

        return orderMapper.selectByOrderId(orderId);
    }

    @Override
    public PageSupport<Order> queryOderByOrderState(String userId,Integer state, Integer pageSize, Integer pageNo,Integer order) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_state",state);
        wrapper.eq("order_uid",userId);
        if(order == 1){
            wrapper.orderByAsc("order_time");
        }else{
            wrapper.orderByDesc("order_time");
        }
        PageHelper.startPage(pageNo,pageSize);
        List<Order> orders = orderMapper.selectList(wrapper);
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        PageSupport<Order> pageSupport = new PageSupport<>();
        // 设置分页内容
        pageSupport.setPageData(orders);
        // 设置当前页码
        pageSupport.setPageNo(pageNo);
        // 设置每页条数
        pageSupport.setPageSize(pageSize);
        // 设置总页数
        pageSupport.setTotalPage(pageInfo.getPages());
        // 设置总记录数
        pageSupport.setTotalCount((int) pageInfo.getTotal());
        return pageSupport;
    }

    @Override
    public PageSupport<Order> queryAllOderByuid(String userId, Integer pageSize, Integer pageNo,Integer order) {
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_uid",userId);
        if(order == 1){
            orderQueryWrapper.orderByAsc("order_time");
        }else{
            orderQueryWrapper.orderByDesc("order_time");
        }

        PageHelper.startPage(pageNo,pageSize);
        List<Order> orders = orderMapper.selectList(orderQueryWrapper);

        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        PageSupport<Order> pageSupport = new PageSupport<>();
        // 设置分页内容
        pageSupport.setPageData(orders);
        // 设置当前页码
        pageSupport.setPageNo(pageNo);
        // 设置每页条数
        pageSupport.setPageSize(pageSize);
        // 设置总页数
        pageSupport.setTotalPage(pageInfo.getPages());
        // 设置总记录数
        pageSupport.setTotalCount((int) pageInfo.getTotal());
        return pageSupport;

    }
}
